package com.ys.controller.web.account;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.account.TcwAdvanceAccount;
import com.ys.entity.account.TcwAdvanceAccountDetail;
import com.ys.service.account.TcwAdvanceAccountDetailService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预支账户明细
 */
@Controller
@RequestMapping("/web/account/tcwAdvanceAccountDetail")
public class WebTcwAdvanceAccountDetailController {
	private static final Logger logger = LoggerFactory.getLogger(WebTcwAdvanceAccountDetailController.class);

	@Autowired
	private CommonApiService<TcwAdvanceAccountDetail> commonApiService;



    @Autowired
    private TcwAdvanceAccountDetailService tcwAdvanceAccountDetailService;

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            model.addAttribute("onProcess", 0);
            Map<String,Object> entity = getEntity(id);
            Long menuId =  MenuId.TCW_AdvanceAccountDetail;
            if(entity.get("transferStatus")!=null && entity.get("transferStatus").toString().equals("1")){//已划拨
                menuId =  MenuId.TCW_AdvanceAccountDetailByTransferStatus;
            }
            if(entity.get("detailType")!=null && entity.get("detailType").toString().equals("1")){//已划拨
                menuId =  MenuId.TCW_AdvanceAccountDetailByDetailType;
            }

            commonApiService.toEdit(model,id, new TcwAdvanceAccountDetail(), menuId, entity, null);
            return "account/tcwAdvanceAccountDetail/toEdit";
        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    /*
     * 详情
     */
    @GetMapping("/toDetail")
    public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            model.addAttribute("onProcess", 1);
            Map<String,Object> entity = getEntity(id);
            return commonApiService.toDetail(model,id, new TcwAdvanceAccountDetail(), MenuId.TCW_AdvanceAccountDetail, entity, null);
        }catch (Exception e) {
            logger.error("详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    public Map<String, Object> getEntity(Long id){
        Map<String,Object> entity = new TcwAdvanceAccountDetail()
                .addLeftJoin("TCW_AdvanceAccount P ON P.Id = A.Pid")//预支账户管理
                .addField("P.Code AS pCode")//预支账户编码
                .addField("P.Name AS pName")//预支账户名称

                .addLeftJoin("TB_AdvanceType T ON T.Code = A.AdvanceTypeCode")//预支类型
                .addField("T.Name AS advanceTypeName")//预支类型名称

                .addLeftJoin("TB_CompanyInfo C ON C.Vid = A.CompanyInfoVid")//公司信息表
                .addField("C.Vname AS companyName")//公司名称

                .addLeftJoin("THR_OperateCompanyApply OC ON OC.Vgsbm = A.Vgsbm")//运营公司表
                .addField("OC.Vgsjc AS vgsjc")//公司简称

                .addLeftJoin("TS_UserChange U ON U.Id = A.CreateChangeId ")
                .addField("U.UserId + '/' + U.UserName AS userName")//新增人员
                .addField("U.DeptName AS deptName")//新增部门
                .setId(id)
                .queryFirstMap();

        return entity;
    }



}






